//
//  UIView+HDEditDeleteSheet.m
//  Erecycling
//
//  Created by pro on 2016/11/29.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "HDEditDeleteSheet.h"


@interface HDEditDeleteSheet ()<UIGestureRecognizerDelegate,UITextFieldDelegate>

@property (nonatomic,strong) NSDictionary * params;
@property (nonatomic,assign) BOOL isShow;
@property (nonatomic,strong) NSString * sign;//1是订单 2是回购车

@property (nonatomic,strong) UITextField * edit_btn;

@end

@implementation  HDEditDeleteSheet

-(id)initWithParams:(NSDictionary *)params Sign:(NSString*)sign
{
    if (self = [super init]) {
        
        self.frame = [UIApplication sharedApplication].keyWindow.screen.bounds;
        self.params = params;
        self.sign = sign;
        
        
        [self initUI];
        
    }
    
    return self;
    
}

-(void)initUI
{
    self.userInteractionEnabled = YES;
    
    transparentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
    transparentView.backgroundColor = [UIColor blackColor];
    transparentView.userInteractionEnabled = YES;
    [transparentView setMultipleTouchEnabled:YES];
    transparentView.alpha = 0.0f;
    [self addSubview:transparentView];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideEditDeleteActionSheet)];
    tap.numberOfTapsRequired = 1;
    [transparentView addGestureRecognizer:tap];
    
    
    UIButton * btn = [[UIButton alloc]initWithFrame:transparentView.frame];
    [btn addTarget:self action:@selector(hideEditDeleteActionSheet) forControlEvents:UIControlEventTouchUpInside];
//    [transparentView addSubview:btn];
    
    
    
    CGFloat sheetWidth = UBO_SCREEN.width - 36*2;
    actionSheetHeight = 110.0f;
    actionSheetView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_1_2 ox:-sheetWidth/2 fy:UB_1_2 oy:-actionSheetHeight/2], [self.ubo ow:sheetWidth oh:actionSheetHeight])];
    actionSheetView.backgroundColor = [UIColor whiteColor];
    actionSheetView.layer.masksToBounds = YES;
    actionSheetView.layer.cornerRadius = UF(10);
    actionSheetView.alpha = 0.95;
    [self addSubview:actionSheetView];
    
    
    __block CGRect frame = CGRectZero;

    frame = actionSheetView.frame;
    frame.size.height = actionSheetHeight;
    actionSheetView.frame = frame;
    
    
    self.edit_btn = [[UITextField alloc]initWithFrame:CGRectOf([actionSheetView.ubo fx:0 ox:12 fy:0], [actionSheetView.ubo ow:sheetWidth-12 oh:actionSheetHeight/2])];
    self.edit_btn.tag = 1;
    self.edit_btn.delegate = self;
    self.edit_btn.placeholder = @"输入您收到的验证码";
    [self.edit_btn setFont:[UIFont systemFontOfSize:14]];
    [actionSheetView addSubview:self.edit_btn];
    
    
    UIButton * delete_btn = [[UIButton alloc]initWithFrame:CGRectOf([self.edit_btn.ufo fx:0 fy:1], [actionSheetView.ubo ow:sheetWidth-12 oh:actionSheetHeight/2])];
    delete_btn.tag = 2;
    [delete_btn setTitle:@"确定" forState:UIControlStateNormal];
    [delete_btn setTitleColor:[XCommon hexStringToColor:kColor_l_light_gray_text] forState:UIControlStateNormal];
    [delete_btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [delete_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [actionSheetView addSubview:delete_btn];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectOf([actionSheetView.ubo fx:0 fy:UB_1_2 ], [actionSheetView.ubo ow:sheetWidth ph:1])];
    line.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [actionSheetView addSubview:line];
    
}


-(void)btnClick:(UIButton*)sender
{
    
    [MBProgressHUD showHUDAddedTo:self animated:YES];
    
    switch (sender.tag) {
        case 1:
        {
            //编辑的请求

            
        }
            break;
        case 2:
        {
            //删除的请求
            [self.edit_btn resignFirstResponder];
            [self removeSelf];
            [self hideEditDeleteActionSheet];
            
            if (self.msgCodeBlock) {
                self.msgCodeBlock(self.edit_btn.text);
            }
            
            
        }
            break;
            
        default:
            break;
    }
    
    
    
    
}




- (void)cancleEvent:(id)sender
{
    
    [self hideEditDeleteActionSheet];
}


#pragma mark -
#pragma mark PickerView show/hide

- (void)hideEditDeleteActionSheet
{
   
    NSLog(@"11111");
    
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.0;
                         
//                         self.center = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame) / 2.0);
                         
                         //self.frame = CGRectMake(0, KDeviceHeight, kDeviceWidth, KDeviceHeight - 64);
                         
                         [transparentView removeFromSuperview];
                         [self removeSelf];
                         
                     } completion:^(BOOL finished) {
                         
                         
                         
                     }];
}

- (void)removeSelf
{
    [self removeFromSuperview];
    
    self.isShow = NO;
    
}

- (void)showEditDeleteActionSheet
{
    [self showEditDeleteActionSheetInView:kWindow];
}

- (void)showEditDeleteActionSheetInView:(UIView *)view
{
//    CGFloat vH = CGRectGetHeight(view.frame);
    
    self.frame = CGRectMake(0, 0, UBO_SCREEN.width, UBO_SCREEN.height);
//    actionSheetY = vH - actionSheetHeight;
    
    [view addSubview:self];
//    [view insertSubview:transparentView belowSubview:self];
    
    self.isShow = YES;
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.4;
//                         self.frame = CGRectMake(0, actionSheetY, kDeviceWidth, actionSheetHeight);
                     }
                     completion:^(BOOL finished) {}];
}



-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    CGFloat sheetWidth = UBO_SCREEN.width - 36*2;
    actionSheetView.frame = CGRectOf([self.ubo fx:UB_1_2 ox:-sheetWidth/2 fy:UB_1_2 oy:-actionSheetHeight], [self.ubo ow:sheetWidth oh:actionSheetHeight]);
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGFloat sheetWidth = UBO_SCREEN.width - 36*2;
    actionSheetView.frame = CGRectOf([self.ubo fx:UB_1_2 ox:-sheetWidth/2 fy:UB_1_2 oy:-actionSheetHeight/2], [self.ubo ow:sheetWidth oh:actionSheetHeight]);
}









@end
